var isdocked = false;
var box_color = '#D4E9F9';
var box_sticky_color = '#004C97';
var tooltipoffsets = [20, -30]; //additional x and y offset from mouse cursor for tooltips
var fadeinspeed = 200; //duration of fade effect in milliseconds
var rightclickstick = true; //sticky tooltip when user right clicks over the triggering element (apart from pressing "s" key) ?
var stickybordercolors = [box_color, box_sticky_color]; //border color of tooltip depending on sticky state
var stickynotice1 = [""]; //customize tooltip status message \\Press \"s\"", "or right click", "to sticky box
var stickynotice2 = "";

function positiontooltip($, $tooltip, e) {
    var x = e.pageX + tooltipoffsets[0], y = e.pageY + tooltipoffsets[1];
    var tipw = $tooltip.outerWidth(), tiph = $tooltip.outerHeight();
    x = (x + tipw > $(document).scrollLeft() + $(window).width()) ? x - tipw - (tooltipoffsets[0] * 2) : x;
    y = (y + tiph > $(document).scrollTop() + $(window).height()) ? $(document).scrollTop() + $(window).height() - tiph - 10 : y;
    $tooltip.css({ left: x, top: y })
};

function showbox($, $tooltip, e) {
    $tooltip.fadeIn(fadeinspeed);
    positiontooltip($, $tooltip, e);
};

function hidebox($, $tooltip) {
    if (!isdocked) {
        $tooltip.html("<img src='themes/default/images/frame/ajax-loader.gif' alt='' /> ");
        $tooltip.stop(false, true).hide()
        $tooltip.css({ borderColor: box_color }).find('.stickystatus:eq(0)').css({ background: stickybordercolors[0] }).html(stickynotice1)
    }
};

function docktooltip($, $tooltip, e) {
    this.isdocked = true
    $tooltip.css({ borderColor: box_sticky_color }).find('.stickystatus:eq(0)').css({ background: stickybordercolors[1] }).html(stickynotice2)
};

function ShowTooltip(object, tooltip, urlTooltip) {
    var $targets = $(object);
    var $tooltip = tooltip.appendTo(document.body);
    if ($targets.length == 0)
        return;
    if (!rightclickstick)
        stickynotice1[1] = '';
    stickynotice1 = "";
    hidebox($, $tooltip);
    if (urlTooltip) {
        $targets.live('mouseenter', function (e) {
            $tooltip.show();
            jQuery.ajax({
                url: urlTooltip,
                type: "POST",
                datatype: "json",
                data: ({
                    'id': $(this).attr('id')
                }),
                success: function (mess) {
                    if(LGCMS.isJsonString(mess)){
                        var obj = $.parseJSON(mess);
                        if(obj.expired != null)
                            window.location = obj.expired;
                    }
                    else{
                        if (mess != "")
                            $tooltip.html(mess);
                        else
                            $tooltip.hide();
                    }
                }
            });
            showbox($, $tooltip, e);
        });
    } else {
        $targets.live('mouseenter', function (e) {
            var msg = $(this).html();
            if (msg && msg != '&nbsp;') {
                $tooltip.show();
                $tooltip.html(msg);
                showbox($, $tooltip, e);
            }
        });
    }
    $targets.live('mouseleave', function (e) {
        hidebox($, $tooltip);
    });
    $targets.live('mousemove', function (e) {
        if (!isdocked) {
            positiontooltip($, $tooltip, e);
        }
    });
    $tooltip.bind("mouseenter", function () {
        hidebox($, $tooltip)
    });
    $tooltip.bind("click", function (e) {
        e.stopPropagation()
    });
    $("body").bind("click", function (e) {
        if (e.button == 0) {
            isdocked = false;
            hidebox($, $tooltip);
        }
    });
    $targets.live("contextmenu", function (e) {
//        if (rightclickstick && $(e.target).parents().andSelf().filter(object).length == 1) { //if oncontextmenu over a target element
//            docktooltip($, $tooltip, e);
//            return false;
//        }
        docktooltip($, $tooltip, e);
        return false;
    });
    object.live('keypress', function (e) {
        var keyunicode = e.charCode || e.keyCode
        if (keyunicode == 115) { //if "s" key was pressed
            docktooltip($, $tooltip, e);
        }
    });
};

